package com.newtribe.net;

import java.io.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
import java.util.*;
import javax.net.ssl.*;

import com.newtribe.security.util.FilePathUtil;


public class SSL
{


	String url="";
	myX509TrustManager xtm = new myX509TrustManager();
	myHostnameVerifier hnv = new myHostnameVerifier();

	public static void main(String [] argvs ) throws Exception {

		//  HttpSsl ssl =new HttpSsl("https://www.verisign.com") ;
		SSL ssl =new SSL("https://mrs.99bill.com/") ;

		String result = ssl.getResult() ;
		System.out.println(result);

	}

	SSLContext sslContext = null;
	public SSL(String HttpsUrl) {
		this.url = HttpsUrl;

		try {
			sslContext = SSLContext.getInstance("TLS");

			X509TrustManager[] trustManager = new X509TrustManager[] { xtm };

			/////////
			//String trustfile ="D:/apache-tomcat-6.0.16/apache-tomcat-6.0.16/conf/keystore.jks" ;
			
			String trustfile=FilePathUtil.getUserDir() +"/src/designs/test.jks";

			KeyStore truststore=KeyStore.getInstance("JKS");

			truststore.load(new FileInputStream(trustfile),"111111".toCharArray());
			TrustManagerFactory trust =TrustManagerFactory.getInstance("SunX509") ;
			trust.init(truststore);


			KeyStore ks=KeyStore.getInstance("JKS");
			ks.load(new FileInputStream(trustfile),"111111".toCharArray());
			KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
			kmf.init(ks,"111111".toCharArray());

			///////////////

			//  sslContext.init( kmf.getKeyManagers(),trust.getTrustManagers(),
			//  new java.security.SecureRandom() );


			javax.net.ssl.ManagerFactoryParameters s =null ;


			sslContext.init( kmf.getKeyManagers(),trustManager,
					new java.security.SecureRandom() );

		} catch( Exception gse ) {
			
			gse.printStackTrace();
		}
		if( sslContext != null ) {
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory() );
			HttpsURLConnection.setDefaultAllowUserInteraction(true) ;
			SSLSessionContext ss=sslContext.getServerSessionContext() ;




			SSLParameters paras =  sslContext.getDefaultSSLParameters();
			String[] suits =paras.getCipherSuites();
			String []  s =paras.getProtocols();
			System.out.println();
		}
		HttpsURLConnection.setDefaultHostnameVerifier( hnv );
	}



	public String getResult() {
		String res = "";
		try {

			URLConnection urlCon = (new URL(url)).openConnection();

		//	SSLSessionContext sss= this.sslContext.getServerSessionContext() ;


			BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
			String line;
			while ((line = in.readLine()) != null)
			{
				//System.out.println(line);
				res += line;
			}

			//  ����Լ��Ĵ���
		} catch( MalformedURLException mue ) {
			mue.printStackTrace();
		} catch( IOException ioe ) {
			ioe.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return res;


	}
}






class myX509TrustManager implements X509TrustManager{
	public myX509TrustManager(){


	}



	public void checkClientTrusted(X509Certificate[] chain,  String authType) throws CertificateException {

		System.out.println("client cert: " + chain[0].toString() + ", authType: " + authType);

	}
	public void checkServerTrusted(X509Certificate[] chain,String authType) throws CertificateException {

		 for(int i =0 ;i<chain.length;i++) {
		   System.out.println("server cert: ====================== "+i+"\n subject: " + chain[i].getSubjectDN()+"\nissue: "+chain[i].getIssuerDN() + "\n, authType: " + authType);
		
		 }
	}
	public X509Certificate[] getAcceptedIssuers() {

		try {
			InputStream in = new FileInputStream("d:/ca/root.cer") ;
			CertificateFactory certFactory = CertificateFactory.getInstance("X.509") ;

			X509Certificate x =  (X509Certificate)certFactory.generateCertificate(in) ;
			System.out.println("============= trust:\n"+x.toString());
			return new X509Certificate[]{x};
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CertificateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null ;
	}
}


class myHostnameVerifier implements HostnameVerifier{
	public myHostnameVerifier(){}
	public boolean verify(String hostname,SSLSession session) {
		System.out.println("hostname: " + hostname);
		return true;
	}
}
